import 'package:dio/dio.dart';
import 'package:puppy_store/models/order_payment_model.dart';
import 'package:puppy_store/service/requests.dart';

class OrderPaymentAPI {
  // 取消订单
  static Future<OrderInfoModel> orderCancel(
    String orderId,
    String? cancelReason,
  ) async {
    String path = '/member/order/$orderId/cancel';
    Map data = {'cancelReason': cancelReason ?? ''};
    Response response = await PuppyRequestManager().handleRequest(
      path,
      'PUT',
      data: data,
    );
    Map res = response.data['result'];

    OrderInfoModel orderInfoModel = OrderInfoModel.fromjson(res);
    return orderInfoModel;
  }

  // 获取订单列表
  static Future<OrderListModel> orderList({
    int? page = 1,
    int? pageSize = 10,
    int? orderState = 0,
  }) async {
    // String path =
    //     '/member/order?page=$page&pageSize=$pageSize&orderState=$orderState';

    String path = '/member/order';
    Map<String, dynamic> queryParameters = {
      'page': page,
      'pageSize': pageSize,
      'orderState': orderState,
    };
    Response response = await PuppyRequestManager().handleRequest(
      path,
      'GET',
      queryParameters: queryParameters,
    );
    Map orders = response.data['result'];

    OrderListModel orderListModel = OrderListModel.fromjson(orders);
    return orderListModel;
  }

  // 订单详情
  static Future<Map> orderDetail(String orderId) async {
    String path = '/member/order/$orderId';
    Response response = await PuppyRequestManager().handleRequest(path, 'GET');
    Map orderDetail = response.data['result'];
    return orderDetail;
  }

  // 支付宝支付
  static Future<String> orderAlipay(String orderId) async {
    String path = '/pay/aliPay/app?orderId=$orderId';
    Response response = await PuppyRequestManager().handleRequest(path, 'GET');
    String orderString = response.data['result'];
    return orderString;
  }

  /// 提交订单
  static Future<Map> orderCommit({
    List? goods,
    String? addressId,
    int? deliveryTimeType,
    int? payType = 1,
    int? payChannel = 1,
    String? buyerMessage,
  }) async {
    Map data = {
      'goods': goods,
      'addressId': addressId,
      'deliveryTimeType': deliveryTimeType,
      'payType': payType,
      'payChannel': payChannel,
      'buyerMessage': buyerMessage,
    };
    Response response = await PuppyRequestManager().handleRequest(
      '/member/order',
      'POST',
      data: data,
    );
    Map ret = response.data['result'];

    return ret;
  }

  // 立即购买
  static Future<OrderSettlementModel> orderBuyNow(
    String skuId,
    int count,
  ) async {
    String path = '/member/order/pre/now?skuId=$skuId&count=$count';
    Response response = await PuppyRequestManager().handleRequest(path, 'GET');
    Map res = response.data['result'];
    // 转模型
    OrderSettlementModel settlementModel = OrderSettlementModel.fromjson(res);
    return settlementModel;
  }

  /// 获取订单结算信息
  static Future<OrderSettlementModel> orderSettlement() async {
    String path = '/member/order/pre';
    Response response = await PuppyRequestManager().handleRequest(path, 'GET');
    Map res = response.data['result'];

    // 转模型
    OrderSettlementModel settlementModel = OrderSettlementModel.fromjson(res);
    return settlementModel;
  }
}
